package com.zzyl.controller;

import com.aliyun.iot20180120.models.QueryDeviceDetailRequest;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/iot")
@Api(tags = "设备管理")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/syncProductList")
    @ApiOperation("同步所有产品")
    public ResponseResult<Void> syncProductList() throws Exception {
        deviceService.syncProductList();
        return ResponseResult.success();
    }

    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品")
    public ResponseResult<List<ProductVo>> allProduct(){
        List<ProductVo> voList = deviceService.allProduct();

        return ResponseResult.success(voList);
    }

    @PostMapping("/RegisterDevice")
    @ApiOperation("新增设备")
    public ResponseResult<Void> registerDevice(@RequestBody DeviceDto deviceDto){
        deviceService.registerDevice(deviceDto);

        return ResponseResult.success();
    }

    @GetMapping("/pageQueryDevice")
    @ApiOperation(value = "分页查询设备列表")
    public ResponseResult<PageResponse<DeviceVo>> pageQueryDevice(DevicePageQueryDto devicePageQueryDto) {
        PageResponse<DeviceVo> pageResponse = deviceService.pageQueryDevice(devicePageQueryDto);
        return ResponseResult.success(pageResponse);
    }

    @PostMapping("/QueryDeviceDetail")
    @ApiOperation(value = "查询设备详细数据")
    public ResponseResult<DeviceVo> queryDeviceDetail(@RequestBody QueryDeviceDetailRequest request) throws Exception {
        DeviceVo deviceVo = deviceService.queryDeviceDetail(request);
        return ResponseResult.success(deviceVo);
    }


    @PostMapping("/QueryThingModelPublished")
    @ApiOperation(value = "查看指定产品的已发布物模型中的功能定义详情")
    @ApiImplicitParam(name = "dto",value = "设备DTO对象",required = true,dataType = "DeviceDto")
    @ApiOperationSupport(includeParameters = {"dto.productKey"})
    public ResponseResult queryThingModelPublished(@RequestBody DeviceDto dto) throws Exception {
        return deviceService.queryThingModelPublished(dto);
    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    @ApiImplicitParam(name = "dto",value = "设备DTO对象",required = true,dataType = "DeviceDto")
    @ApiOperationSupport(includeParameters = {"dto.productKey","dto.deviceName"})
    public ResponseResult queryDevicePropertyStatus(@RequestBody DeviceDto dto) throws Exception {

        return deviceService.queryDevicePropertyStatus(dto);

    }
}
